/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.database.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.log4j.Logger;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Corvus 11
 */
@Component
public abstract class AbstractDao {
    protected static final Logger LOG = Logger.getLogger("Dao");
    
    //значение ID не определено
    public static final long ID_NOT_DEFINED = 0l;
    public static final long ID_INVALID = -1l;
    
    //направление сортировки
    protected static final int SORT_ORDER_ASC = 0;
    protected static final int SORT_ORDER_DESC = 1;
    
    @Autowired
    protected DataSource dataSource;
       
    @PostConstruct
    public void initIt() {
        LOG.info("Инициализация AbstractDao");

        dataSource.setInitSQL("USE equipment;");
    }
    
    protected void safeClose(Connection c) {
        if (c != null) {
            try {
                c.close();
            } catch (SQLException exc) {
                LOG.error("safeClose() - error with closing of connection.",exc);
            }
        }
    }
    
    protected void safeClose(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException exc) {
                LOG.error("safeClose() - error with closing of result set.",exc);
            }
        }
    }
    
    protected void safeClose(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException exc) {
                LOG.error("safeClose() - error with closing of statement.",exc);
            }
        }
    }
    
    protected void appendSortOrder(StringBuilder sqlBuilder, String fieldName, int orderType) {
        sqlBuilder.append(" ORDER BY ");
        sqlBuilder.append(fieldName);
        if (orderType == SORT_ORDER_DESC) sqlBuilder.append(" DESC");
    }

    protected void appendSortOrder(StringBuilder sqlBuilder, String fieldName, boolean orderAsc) {
        sqlBuilder.append(" ORDER BY ");
        sqlBuilder.append(fieldName);
        if (!orderAsc) sqlBuilder.append(" DESC");
    }
    
    protected void appendPaginationParams(StringBuilder sqlBuilder, int from_index, int count) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(from_index);
        sqlBuilder.append(", ");
        sqlBuilder.append(count);
    }
    
    protected boolean deleteById(String tableName, long id) {
        
        StringBuilder sql = new StringBuilder();
        
        sql.append("DELETE FROM ");
        sql.append(tableName);
        sql.append(" WHERE id = ?;");
        
        boolean ok = false;
        Connection conn = null;
        PreparedStatement st = null;
        
        try {
            conn = dataSource.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setLong(1, id);
            ok = st.executeUpdate() > 0;
        } catch (SQLException exc) {
            LOG.error("AbstractDao.deleteById() error",exc);
        }
        
        return ok;
    }

}
